# -*- coding: utf-8 -*-

from dbobject import DBObject
import yaml

class Reference(DBObject):

	def __init__(self, referenceKey=''):

		DBObject.__init__(self)

		self.key = referenceKey

		#string
		self._things['title'] = ""
		self._things['author_name'] = ""
		self._things['source'] = ""
		self._things['abstract'] = ""

		#int
		self._things['volume'] = 0
		self._things['issue'] = 0
		self._things['year'] = 1800
		self._things['pageStart'] = 0
		self._things['pageEnd'] = 0


	def _loadYAML(self, yml):

		data = yaml.load(yml)

		if "authors" in data.keys():
			authors = data["authors"]
			for name in authors:
				self._things["author_name"] += name.strip()+'; '
			self._things["author_name"] = self._things["author_name"][:-2] # drop the trailing semicolon and space
			del data["authors"]

		DBObject._loadYAML(self, data)

	def _dumpYAML(self, properties=[]):
		#print "Trying to export a reference"

		if properties:
			keys = properties
		else:
			keys = self.properties().keys()

		data = {}

		if "author_name" in keys:
			keys.remove("author_name")
			data["authors"] = []
			for name in self._things["author_name"].split(';'):
				data["authors"].append(name.strip())
		
		rest = list(set(keys) - set(data.keys()))
		if not rest:
			rest = [None]

		#print "Aux data: ", data
		return DBObject._dumpYAML(self, otherData=data, properties=rest)